﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FJERP.Model;
using System.Data;
using System.Collections;
using System.Data.SqlClient;
using FJERP.Model;
using System.Net;

namespace FJERP.DAL
{
    public class EmployeeDAL
    {
        SQLHelper heper = new SQLHelper();
        /// <summary>
        /// 显示员工信息
        /// </summary>
        /// <returns></returns>
        public string Empoyee_Show()
        {
            string sql = "Select Employee_Id,Employee_Name,Employee_Pwd,Employee_Phone,Employee_State,Employee_Addr,Employee_Remark from dbo.Employee";
            DataTable table = heper.ExecDataTable(sql);
            System.Web.Script.Serialization.JavaScriptSerializer js = new System.Web.Script.Serialization.JavaScriptSerializer();
            ArrayList list = new ArrayList();
            foreach (DataRow item in table.Rows)
            {
                System.Collections.Generic.Dictionary<string, object> dic = new Dictionary<string, object>();
                foreach (DataColumn cl in table.Columns)
                {
                    dic.Add(cl.ColumnName, item[cl.ColumnName]);
                }
                list.Add(dic);
            }
            return js.Serialize(list);

        }
        /// <summary>
        /// 显示员工信息
        /// </summary>
        /// <returns></returns>
        public List<Employee> Empoyee_Show(int i)
        {
            string sql = "Select Employee_Id,Employee_Name,Employee_Pwd,Employee_Phone,Employee_State,Employee_Addr,Employee_Remark from dbo.Employee";
            DataTable table = heper.ExecDataTable(sql);
            List<Employee> list = new List<Employee>();
            foreach (DataRow item in table.Rows)
            {
                Employee m = new Employee();
                m.Employee_Id = Convert.ToInt32(item["Employee_Id"]);
                m.Employee_Name = item["Employee_Name"].ToString();
                m.Employee_Pwd = item["Employee_Pwd"].ToString();
                m.Employee_Phone = item["Employee_Phone"].ToString();
                m.Employee_Addr = item["Employee_Addr"].ToString();
                m.Employee_State = Convert.ToInt32(item["Employee_State"]);
                m.Employee_Remark = item["Employee_Remark"].ToString();
                list.Add(m);
            }
            return list;

        }
        /// <summary>
        /// 根据ID获取信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public DataTable Empoyee_Hu(int id)
        {
            string sql = "Select Employee_Id,Employee_Name,Employee_Pwd,Employee_Phone,Employee_Addr,Employee_State,Employee_Remark from dbo.Employee where Employee_Id=" + id + "";
            return heper.ExecDataTable(sql);
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public int Empoyee_Upt(Employee e)
        {
            string sql = "update Employee set Employee_Name='" + e.Employee_Name + "',Employee_Pwd='" + e.Employee_Pwd + "',Employee_Phone='" + e.Employee_Phone + "',Employee_State='" + e.Employee_State + "',Employee_Remark='" + e.Employee_Remark + "',Employee_Addr='" + e.Employee_Addr + "'  where Employee_Id=" + e.Employee_Id + "";
            return heper.ExecNonquery(sql);
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public int Empoyee_Del(int id)
        {
            string sql = "delete from Employee where Employee_Id=" + id + "";
            return heper.ExecNonquery(sql);
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="e"></param>
        /// <returns></returns>
        public int Empoyee_Add(Employee e)
        {
            string sql = "insert into Employee(Employee_Name,Employee_Pwd,Employee_Phone,Employee_State,Employee_Remark,Employee_Addr) values('" + e.Employee_Name + "','" + e.Employee_Pwd + "','" + e.Employee_Phone + "','" + e.Employee_State + "','" + e.Employee_Remark + "','" + e.Employee_Addr + "')";
            return heper.ExecNonquery(sql);
        }
        /// <summary>
        /// 登录方法
        /// </summary>
        /// <param name="name"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        public int login(string name, string pwd)
        {
            string AddressIP = string.Empty;
            foreach (IPAddress _IPAddress in Dns.GetHostEntry(Dns.GetHostName()).AddressList)
            {
                if (_IPAddress.AddressFamily.ToString() == "InterNetwork")
                {
                    AddressIP = _IPAddress.ToString();
                }
            }
            string sql = "Journal_Show";
            SqlParameter[] Par = new SqlParameter[] { 
                         new SqlParameter("@Employee_Name",name),
                         new SqlParameter("@Employee_Pwd",pwd),
                         new SqlParameter("@Journa_Date",DateTime.Now),
                         new SqlParameter("@JournalIP",AddressIP),
           
           };
            return Convert.ToInt32(heper.ExecScalar_Proc(sql, Par));
        }
    }
}
